<template>
  <div>
    <el-dialog
      :title="diaInfo.isAdd ? '添加轮播图' : '编辑录播图'"
      :visible.sync="diaInfo.isShow"
      center
      :before-close="cancel"
    >
      <el-form :model="bannerForm" ref="bannerRef" :rules="rules">
        <el-form-item prop="title" label="标题" :label-width="formLabelWidth">
          <el-input v-model="bannerForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :on-change="changeInfo"
            :file-list="fileList"
            :limit="1"
            :auto-upload="false"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-tooltip :content="'Switch value: ' + value" placement="top">
            <el-switch
              v-model="bannerForm.status"
              active-color="#13ce66"
              inactive-color="#ccc"
              :active-value="1"
              :inactive-value="2"
            >
            </el-switch>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { bannerAdd, bannerInfo, bannerEdit } from "../../request/api.js";
import { mapActions } from "vuex";
export default {
  props: ["diaInfo"],
  data() {
    return {
      dialogFormVisible: false,
      bannerForm: {
        title: "", //轮播图名称
        img: "", //图片(文件)
        status: 1, //状态  1正常2禁用
      },
      formLabelWidth: "120px",
      dialogImageUrl: "",
      dialogVisible: false,
      value: "1",
      fileList: [], // 用于清空图片展示
      rules: {
        title: [
          { required: true, message: "请输入标题名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 辅助函数
    ...mapActions({
      getBannerListactions: "banner/getBannerListactions",
    }),
    // 封装一个取消
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    onPreview(file) {
      // console.log(file,'11111');
      // 打开预览弹框
      this.dialogVisible = true;
      // 给弹框赋值图片地址
      this.dialogImageUrl = file.url;
    },
    // 文件改变时触发的事件
    changeInfo(file, filelist) {
      // console.log(file,filelist,111);
      this.bannerForm.img = file.raw;
    },
    // 封装一个清空事件
    reset() {
      (this.bannerForm = {
        title: "", //轮播图名称
        img: "", //图片(文件)
        status: 1, //状态  1正常2禁用
      }),
        this.$refs.bannerRef.resetFields();
      this.fileList = [];
    },
    // 封装一个数据回显
    lookup(id) {
      // console.log(id);
      bannerInfo({ id }).then((res) => {
        // console.log(res);
        if (res.code == 200) {
          this.bannerForm = res.list;
          this.bannerForm.id = id;
          // 图片回显
          this.fileList = this.bannerForm.img ? [{ url: this.bannerForm.img }] : [];
        }
      });
    },
    // 添加
    confirm() {
      this.$refs.bannerRef.validate((val) => {
        if (val) {
          // console.log(this.bannerForm);
          if (this.diaInfo.isAdd) {
            bannerAdd(this.bannerForm).then((res) => {
              if (res.code == 200) {
                // console.log(res);
                this.cancel();
                this.$message.success(res.msg);
                this.getBannerListactions();
              }
            });
          } else {
            bannerEdit(this.bannerForm).then((res) => {
              if (res.code == 200) {
                // console.log(res);
                this.cancel();
                this.$message.success(res.msg);
                this.getBannerListactions();
              }
            });
          }
        }
      });
    },
  },
  mounted() {},
  watch: {},
  computed: {},
  filters: {},
  components: {},
};
</script>
<style scoped></style>
